﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Region.Forms
{
    public partial class RegionKontList : Form
    {
        private Boolean m_Selectable;
        private Int32 m_SelectKontID;
        public Boolean Selectable { get { return m_Selectable; } set { m_Selectable = value; SelectButton.Visible = m_Selectable; } }
        public Int32 SelectKontID { get { return m_SelectKontID; } }
        private Engine m_engine;
        private KontList m_kontList;
        Properties.Settings m_ps;
        public RegionKontList(Engine engine)
        {
            m_engine = engine;
            m_ps = Properties.Settings.Default;
            m_kontList = new KontList(m_engine);       
            InitializeComponent();
            LineInPage.Text = m_ps.KontReestrLineInPage.ToString();
            this.DialogResult = DialogResult.Cancel;
            m_SelectKontID = 0;
        }
        private void ReestrList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void Filter_Click(object sender, EventArgs e)
        {
            this.ReestrList.Items.Clear();
            m_kontList.setWhere("LineInPage", LineInPage.Text);
            m_kontList.setWhere("Find", Find.Text);
            /*m_rasporList.setWhere("RasporYear", RasporYear.Text);
            
            m_rasporList.setWhere("RasporNumStart", RasporNumStart.Text);
            m_rasporList.setWhere("RasporNumEnd", RasporNumEnd.Text);
            m_rasporList.setWhere("IspUserID", null); for (int i = 0; i < m_IspUserFIO.Length; i++) if (m_IspUserFIO[i] == IspUserList.Text) { m_rasporList.setWhere("IspUserID", m_IspUserID[i]); break; }
            m_rasporList.setWhere("ParentOtdelID", OtdelList.SelectItemID());*/
            m_kontList.Filter();
            Int32 Num = m_kontList.NumLines;
            for (int i = 0; i < Num; i++)
            {
                KontListLine line = m_kontList.getLine(i);
                ListViewItem R = new ListViewItem(line.KontID.ToString());
                R.Tag = line.KontID;
                R.SubItems.Add(line.KontID.ToString());
                R.SubItems.Add(line.INN);
                R.SubItems.Add(line.OGRN);
                R.SubItems.Add(line.KontName);
                if (line.My) R.Group = this.ReestrList.Groups["My"]; else R.Group = this.ReestrList.Groups["All"];
                this.ReestrList.Items.Add(R);
            }
        }

        private void RegionKontList_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                m_ps.KontReestrLineInPage = Int32.Parse(LineInPage.Text);
                m_ps.Save();
            }
            finally { }
        }

        private void ReestrList_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            m_SelectKontID = 0;
            try
            {
                if (ReestrList.SelectedItems.Count > 0)
                {
                    m_SelectKontID = (int)ReestrList.SelectedItems[0].Tag;
                    //this.DialogResult = DialogResult.OK;
                    SelectButton.Enabled = true;
                    Edit.Enabled = true;
                    return;
                }
            }
            catch{}
            //this.DialogResult = DialogResult.Cancel;
            m_SelectKontID = 0;
            SelectButton.Enabled = false;
            Edit.Enabled = false;
        }

        private void Select_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var Import = new Forms.ImportEgrip(m_engine);            
            Import.ShowDialog();
            if (Import.KontID > 0)
            {
                KontForm KF = new KontForm(m_engine, Import.KontID);
                KF.ShowDialog();
                Kont K = m_engine.getKont(Import.KontID);
                Find.Text = K.OGRN;
                Filter_Click(this, null);
            }
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            if (m_SelectKontID > 0)
            {
                KontForm KF = new KontForm(m_engine, m_SelectKontID);
                this.Visible = false;
                KF.ShowDialog();
                this.Visible = true;
            }
        }

        private void ReestrList_DoubleClick(object sender, EventArgs e)
        {
            if (m_SelectKontID > 0)
            {
                KontForm KF = new KontForm(m_engine, m_SelectKontID);
                this.Visible = false;
                KF.ShowDialog();
                this.Visible = true;
            }
        }
    }
}
